package database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MySQLOperatoerDAO implements OperatoerDAO {

	private Connector conn = null;
	private ResultSet rs = null;

	public MySQLOperatoerDAO() throws DALException {
		conn = new Connector();
	}

	public OperatoerDTO getOperatoer(int oprId) throws DALException {
		if (conn == null)
			conn = new Connector();
		OperatoerDTO opr = null;

		String query = "SELECT * FROM operatoer WHERE oprId = " + oprId + ";";
		rs = conn.doQuery(query);
		try {
			if (!rs.first()) {
				throw new DALException("Operatoeren med oprId " + oprId
						+ " findes ikke");
			}
			opr = new OperatoerDTO(rs.getInt(1), rs.getString(2),
					rs.getString(3), rs.getString(4), rs.getString(5),
					rs.getInt(6));
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return opr;
	}

	public OperatoerDTO getOperatoer(String initials) {
		if (conn == null)
			try {
				conn = new Connector();
			} catch (DALException e1) {
				return null;
			}
		OperatoerDTO opr = null;

		String query = "SELECT * FROM operatoer WHERE ini = \"" + initials + "\";";
		try {
			rs = conn.doQuery(query);
		} catch (DALException e1) {
			return null;
		}
		try {
			if (!rs.first()) {
				return null;
			}
			opr = new OperatoerDTO(rs.getInt(1), rs.getString(2),
					rs.getString(3), rs.getString(4), rs.getString(5),
					rs.getInt(6));
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return opr;
	}

	public ArrayList<OperatoerDTO> getOperatoerList() throws DALException {
		ArrayList<OperatoerDTO> oprList = new ArrayList<OperatoerDTO>();
		String query = "SELECT * FROM operatoer";
		rs = conn.doQuery(query);
		try {
			while (rs.next()) {
				oprList.add(new OperatoerDTO(rs.getInt(1), rs.getString(2), rs
						.getString(3), rs.getString(4), rs.getString(5), rs
						.getInt(6)));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return oprList;
	}

	public List<OperatoerDTO> getOperatoerList(int first, int count) throws DALException {
		List<OperatoerDTO> oprList = new ArrayList<OperatoerDTO>();
		String query = "SELECT * FROM operatoer LIMIT " + first + "," + count;
		rs = conn.doQuery(query);
		try {
			while (rs.next()) {
				oprList.add(new OperatoerDTO(rs.getInt(1), rs.getString(2), rs
						.getString(3), rs.getString(4), rs.getString(5), rs
						.getInt(6)));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return oprList;
	}

	public int getCount() throws DALException {
		rs = conn.doQuery("SELECT COUNT(oprId) FROM operatoer");
		try {
			if (rs.next())
				return rs.getInt(1);
			else
				return 0;
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}

	public void createOperatoer(OperatoerDTO opr) throws DALException {
		if (conn == null)
			conn = new Connector();
		conn.doUpdate("INSERT INTO operatoer (oprId, oprNavn, ini, cpr, kodeord, rolle) VALUES"
				+ "(0,'"
				+ opr.getOprNavn()
				+ "','"
				+ opr.getIni()
				+ "','"
				+ opr.getCpr()
				+ "','"
				+ opr.getKodeord()
				+ "',"
				+ opr.getRolle() + ");");

	}

	public void updateOperatoer(OperatoerDTO opr) throws DALException {
		conn.doUpdate("UPDATE operatoer SET  oprNavn = '" + opr.getOprNavn()
				+ "', ini =  '" + opr.getIni() + "', cpr = '" + opr.getCpr()
				+ "', kodeord = '" + opr.getKodeord() + "', rolle = '"
				+ opr.getRolle() + "' WHERE oprId = " + opr.getOprId());

	}

	public void disableOperatoer(OperatoerDTO opr) throws DALException {
		conn.doUpdate("UPDATE operatoer SET rolle = 0  WHERE oprId = "
				+ opr.getOprId());
	}
	
}
